package cn.lxg.controller.admin;

import cn.lxg.pojo.TNovel;
import cn.lxg.pojo.TNovelGroup;
import cn.lxg.service.INovelGroupService;
import cn.lxg.service.INovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller(value = "adminNovelController")
@RequestMapping(value = "/page/admin")
public class NovelController {
    @Autowired
    private final INovelService novelService = null;

    @Autowired
    private final INovelGroupService novelGroupService = null;

    @GetMapping("/novel_list")
    public String listPage(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TNovel novel) {
        // 预处理参数
        if (novel.getName() != null)
            novel.setName(novel.getName().trim());
        if (novel.getSynopsis() != null)
            novel.setSynopsis(novel.getSynopsis().trim());
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        List<TNovel> novelList = this.novelService.getList(limit, page, novel);
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);
        int dataCount = this.novelService.countDataLine(novel);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("novel", novel);
        model.addAttribute("novelList", novelList);
        model.addAttribute("novelGroupList", novelGroupList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "admin/novel/list";
    }

    @GetMapping(value = "/add_novel")
    public String addPage(Model model) {
        // 预处理参数
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);

        // 传递数据
        model.addAttribute("novelGroupList", novelGroupList);

        return "admin/novel/add";
    }

    @PostMapping(value = "/novel")
    public String add(Model model, TNovel novel) {
        // 参数预处理
        novel.setName(novel.getName().trim());
        novel.setSynopsis(novel.getSynopsis().trim());

        // 准备数据
        String msg = this.novelService.add(novel);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/delete_novel/{id}")
    public String deletePage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovel novel = this.novelService.get(id);

        // 传递数据
        model.addAttribute("novel", novel);

        return "admin/novel/delete";
    }

    @DeleteMapping(value = "/novel/{id}")
    public String delete(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        String msg = this.novelService.delete(id);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/edit_novel/{id}")
    public String editPage(Model model, @PathVariable(value = "id") Integer id) {
        // 预处理参数
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        TNovel novel = this.novelService.get(id);
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);

        // 传输数据给前端
        model.addAttribute("novel", novel);
        model.addAttribute("novelGroupList", novelGroupList);

        return "admin/novel/edit";
    }

    @PutMapping(value = "/novel")
    public String edit(Model model, TNovel novel) {
        // 参数预处理
        novel.setName(novel.getName().trim());
        novel.setSynopsis(novel.getSynopsis().trim());

        // 准备数据
        String msg = this.novelService.edit(novel);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/look_novel/{id}")
    public String lookPage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        TNovel novel = this.novelService.get(id);

        // 传输数据给前端
        model.addAttribute("novel", novel);

        return "admin/novel/look";
    }
}
